const foggbuffer_frag =/*wgsl*/`
struct Input {  
    @location(0) uv : vec2<f32>,
    @location(1) fragPosition : vec2<f32>,
};
struct Output {
    @location(0) color:vec4<f32>,
};
struct DepthFog{
    fogType:f32,//0线性雾 1指数雾 2指数平方雾
    density:f32,//雾强度
    start:f32,//雾开始
    end:f32,//雾结束距离
    color:vec3<f32>,//雾颜色
    atten:f32,//雾的衰减
    skyfogSmooth:f32//天空部分的雾平滑(越小越平滑)
}
struct HeightFog{
    fogType:f32,//0线性雾 1指数雾 2指数平方雾
    density:f32,//雾强度
    start:f32,//雾开始
    end:f32,//雾结束距离
    color:vec3<f32>,//雾颜色
    distance:f32//起雾距离
}
struct Camera{
    vp:mat4x4<f32>,
    vpInvert:mat4x4<f32>,
    view:mat4x4<f32>,
    project:mat4x4<f32>,
    rotation:mat4x4<f32>,
    invert:mat4x4<f32>,
    position:vec3<f32>,
    dist:f32,
    viewport:vec2<f32>,
    near:f32,
    far:f32,
    at:vec3<f32>
  }
@group(0) @binding(0) var Sampler: sampler;
@group(0) @binding(1) var Texture: texture_2d<f32>;
@group(0) @binding(2) var TexturePosition: texture_2d<f32>;
@group(0) @binding(3) var<uniform> camera : Camera;
@group(0) @binding(4) var<uniform> depthfog:DepthFog;
@group(0) @binding(5) var<uniform> heightfog:HeightFog;
@fragment
fn main(input:Input) -> Output {
    var output:Output;
    var worldpos:vec4<f32>=textureSample(TexturePosition, Sampler,input.uv);//[0-1]
    var dist:f32=distance(camera.position,worldpos.xyz)/depthfog.atten;
    var viewDis=dist/depthfog.atten;
    //计算世界坐标位置
    //根据世界坐标z添加高度雾（在临界值下和陆地颜色混合，临界值以上和天空颜色混合）
    //根据与相机距离计算深度雾
    //混合高度雾和深度雾
    var color:vec4<f32>=textureSample(Texture, Sampler,input.uv);
    var merge:vec3<f32>=color.xyz;
    if(worldpos.w==-1.0){
        if(worldpos.y>=0){
            let factor:f32=clamp(0.0,1.0,asin(worldpos.y*depthfog.skyfogSmooth));
            output.color=vec4(mix(depthfog.color,merge,factor),color.w);
        }else{
            output.color=vec4(depthfog.color,1.0);
        }     
    }else{
        //高度雾
        if(distance(camera.at,worldpos.xyz)>heightfog.distance){
            var heightFactor:f32=1.0;
            if(heightfog.fogType==1.0){//线性雾
                //fogFactor=saturate((fog.end-viewDis)/(fog.end-fog.start));
                heightFactor=smoothstep(heightfog.start,heightfog.end,worldpos.z);
            }else if(heightfog.fogType==2.0){//指数雾
                heightFactor=1.0-saturate(exp(-heightfog.density*worldpos.z));
            }else if(heightfog.fogType==3.0){//指数平方雾
                heightFactor=1.0-saturate(exp2(-heightfog.density * heightfog.density * worldpos.z * worldpos.z));
            }
            merge=mix(heightfog.color,merge.xyz,heightFactor);
        }
        //深度雾
        var depthFactor:f32=1.0;
        if(depthfog.fogType==1.0){//线性雾
            //fogFactor=saturate((fog.end-viewDis)/(fog.end-fog.start));
            depthFactor=1.0-smoothstep(depthfog.start,depthfog.end,viewDis);
        }else if(depthfog.fogType==2.0){//指数雾
            depthFactor=saturate(exp(-depthfog.density*viewDis));
        }else if(depthfog.fogType==3.0){//指数平方雾
            depthFactor=saturate(exp2(-depthfog.density * depthfog.density * viewDis * viewDis));
        }
        merge=mix(depthfog.color,merge,depthFactor);
        output.color=vec4(merge,color.w);
    }
    return output;
}
`
export { foggbuffer_frag }